package cn.com.unionlife.hy.core.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import cn.com.unionlife.hy.system.vo.UserVO;


public class BaseController {
	
	public final static String LOGINED_USER = "logined_user";
	public final static String PAGINATION_KEY = "pagination";

	/**
	 * 获取登录后的用户
	 * @return
	 */
	public UserVO getLoginedUser(HttpServletRequest request){
		Object ret = request.getSession(true).getAttribute(LOGINED_USER);
		return ret == null ? null : (UserVO)ret;
	}
	
	protected String ajaxDone(int statusCode, String message, String forwardUrl , Model model) {
		model.addAttribute("statusCode", statusCode);
		model.addAttribute("message", message);
		model.addAttribute("forwardUrl", forwardUrl);
		return "/ajaxDone";
	}
	
	@InitBinder  
    protected void initBinder(HttpServletRequest request,  
            ServletRequestDataBinder binder) throws Exception {  
    	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	dateFormat.setLenient(true);
    	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    	binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
    	binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
    	
    	
    }  
	
}
